﻿Imports System.Data.SqlClient
Public Class MenuUsuarioDAL
#Region "Incluir"
    Public Function inserir(men_codigo As Integer, usu_codigo As Integer) As Integer
        Dim cmd = New SqlCommand
        Dim codigo As Integer
        Try
            cmd.CommandText = "insert into Estoque.Ofi_MenuUsuario(men_codigo, usu_codigo) values (@menu, @usuario)"
            cmd.Parameters.Add(New SqlParameter("@menu", SqlDbType.Int)).Value = men_codigo
            cmd.Parameters.Add(New SqlParameter("@usuario", SqlDbType.Int)).Value = usu_codigo
            codigo = objconexao.executaCmd(cmd)
        Catch ex As Exception
            Throw ex
        Finally
            cmd = Nothing
        End Try
        Return codigo
    End Function
#End Region

#Region "Excluir"
    Public Function excluirtodositens(usu_codigo As Integer) As Integer
        Dim cmd = New SqlCommand
        Dim codigo As Integer
        Try
            cmd.CommandText = "delete from Estoque.Ofi_MenuUsuario where usu_codigo=@codigo"
            cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int, 1)).Value = usu_codigo
            codigo = objconexao.executaCmd(cmd)

        Catch ex As Exception
            Throw ex

        Finally
            cmd = Nothing

        End Try
        Return codigo
    End Function
#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim cmd As SqlCommand
        Dim tab As DataTable
        Try
            cmd = New SqlCommand
            Select Case tipo
                Case 1
                    'por codigo
                    cmd.CommandText = "select * from vw_EstMenuUsuario where usu_codigo=@codigo"
                    cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = Val(descricao)

                Case 2
                    'buscar menu dinamico
                    cmd.CommandText = "select * from Estoque.Ofi_Menu"

                Case 3
                    cmd.CommandText = "select * from Estoque.Ofi_Menu where men_nivel=@codigo"
                    cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = Val(descricao)
                Case 4
                    cmd.CommandText = "select * from Estoque.Ofi_Menu where men_nivel=@codigo order by men_descricao"
                    cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = Val(descricao)

                Case Else
                    'todos
            End Select
            tab = objconexao.executaConsulta(cmd)
            tab.DefaultView.AllowNew = False
        Catch ex As Exception
            Throw ex
        Finally
            cmd = Nothing
        End Try
        Return tab
    End Function
#End Region
End Class
